Certified Java Programmer Mock Exam


Question 1


class Red {
  public int a;
  public static int b;
  public static void main (String[] in) {
    Red r1 = new Red();
    Red r2 = new Red();
    r1.a++;
    r1.b++;
    System.out.print(r1.a+", "+r1.b+", "+r2.a+", "+r2.b);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 0, 0, 0, 0
b. Prints: 0, 1, 1, 1
c. Prints: 1, 1, 1, 0
d. Prints: 1, 1, 0, 1
e. Prints: 1, 1, 0, 0
f. Prints: 1, 1, 1, 1
g. Compiler Error
h. Run time Exception
i. None of the Above

Question 2


1.  public class Basics {}
2.  class Basics1 {}
3.  protected class Basics2 {}
4.  private class Basics3 {}
5.  Class Basics4 {}

Assuming these class definitions are contained in a file named Basics.java, what is the result of attempting to compile the contents of the file?

a. Compiler error at line 1.
b. Compiler error at line 2.
c. Compiler error at line 3.
d. Compiler error at line 4.
e. Compiler error at line 5.
f. None of the Above

Question 3


1.  class Basics1 {}
2.  class Basics2 {}
3.  class Basics3 {}
4.  class Basics4 {}

Assuming these class definitions are contained in a file named Basics.java, what is the result of attempting to compile the contents of the file?

a. Compiler error at line 1.
b. Compiler error at line 2.
c. Compiler error at line 3.
d. Compiler error at line 4.
e. None of the Above

Question 4


class A {
  public static void main (String[] args) {
    private int x = 1;
    protected int y = 2;
    public int z = 3;
    System.out.println(x+y+z);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 123
b. Prints: 1 2 3
c. Prints: 6
d. Run time error
e. Compiler Error
f. None of the Above

Question 5



Which of the following modifiers can be applied to a method?

a. abstract
b. final
c. private
d. protected
e. public
f. static
g. synchronized
h. transient
i. volatile
j. native
k. strictfp
l. None of the above.

Question 6



Which of the following statements are true?

a. If the superclass method is static, then any method with the same signature in a subclass must also be static.
b. If the superclass method is strictfp, then the overriding method must also be strictfp.
c. If the superclass method is synchronized, then the overriding method must also be synchronized.
d. If the superclass method is public, then the overriding method must also be public.
e. If the superclass method is native, then the overriding method must also be native.
f. If the superclass method is protected, then the overriding method must be protected or public.
g. None of the above.

Question 7


class E {
  public static void main (String[] args) {
    byte[] a = new byte[1];
    long[] b = new long[1];
    float[] c = new float[1];
    Object[] d = new Object[1];
    System.out.print(a[0]+","+b[0]+","+c[0]+","+d[0]);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 0,0,0,null
b. Prints: 0,0,0.0,null
c. Compiler Error.
d. Runtime Error.
e. None of the Above.

Question 8


abstract class A {}
final class B {}
private class C {}
protected class D {}
static class E {}
synchronized class F {}
transient class G {}
volatile class H {}
strictfp class I {}

Suppose these are top-level class declarations and not nested class declarations. Which of these results in a compiler error?

a. A
b. B
c. C
d. D
e. E
f. F
g. G
h. H
i. I
j. None of the Above

Question 9


Class A is declared in a file named A.java.
1.  package com.dan.chisholm;
2.  
3.  public class A {
4.    public void m1() {System.out.print("A.m1, ");}
5.    protected void m2() {System.out.print("A.m2, ");}
6.    private void m3() {System.out.print("A.m3, ");}
7.    void m4() {System.out.print("A.m4, ");}
8.  }

Class D is declared in a file named D.java.
9.  package com.dan.chisholm.other;
10.  
11. import com.dan.chisholm.A;
12.  
13. public class D {
14.   public static void main(String[] args) {
15.     A a = new A();
16.  	a.m1();
17.  	a.m2();
18. 	a.m3();
19. 	a.m4();
20.   }
21. }

What is the result of attempting to compile and run the above program?

a. Prints A.m1, A.m2, A.m3, A.m3, A.m4,
b. Compiler error at line 16.
c. Compiler error at line 17.
d. Compiler error at line 18.
e. Compiler error at line 19.
f. None of the Above

Question 10



Suppose that the compiler generates a default constructor for a class. If a compile-time error is to be avoided which of the following must be true?

a. The super class must have an accessible constructor that takes no arguments.
b. The no-argument super class constructor must not have a throws clause.
c. None of the above.

Question 11


class A {
  public float a;        // 1
  protected float b;     // 2
  strictfp float h;      // 3
  private float c;       // 4
  static float d;        // 5
  synchronized float i;  // 6
}

Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. None of the above.

Question 12



Which of the following modifiers can be applied to a field?

a. abstract
b. final
c. private
d. protected
e. public
f. static
g. synchronized
h. transient
i. volatile
j. native
k. strictfp
l. None of the above.

Question 13



Suppose that the superclass constructor invocation statement, super, appears explicitly in a subclass constructor. If a compile-time error is to be avoided then the arguments for the superclass constructor invocation statement, super, can not refer to which of the following?

a. Static variables declared in this class or any superclass.
b. Instance variables declared in this class or any superclass.
c. Static methods declared in this class or any superclass.
d. Instance methods declared in this class or any superclass.
e. The keyword "this".
f. The keyword "super".
g. None of the above.

Question 14



Which of the following statements are true?

a. A constructor can invoke another constructor of the same class using the constructor invocation statement "this".
b. A constructor can invoke itself using the constructor invocation statement "this".
c. The constructor invocation statement, "this", can legally appear anywhere in the constructor body.
d. A constructor can invoke the constructor of the direct superclass using the superclass constructor invocation statement "super".
e. The number of constructor invocation statements that may appear in any constructor body can equal but not exceed the number of alternate constructors declared in the same class.
f. A constructor is not permitted to throw an exception.
g. None of the above.

Question 15



Which of the following statements are true?

a. A final method can not be overridden.
b. All methods declared in a final class are implicitly final.
c. The methods declared in a final class must be explicitly declared final or a compile-time error occurs.
d. It is a compile-time error if a private method is declared final.
e. A machine-code generator can inline the body of a final method.
f. None of the above.

Question 16


class A {
  public static void main(String[] args) {
    int[] a1[],a2[];  // 1
    int []a3,[]a4;    // 2
    int []a5,a6[];    // 3
    int[] a7,a8[];    // 4
  }
}

Compile-time errors occur at which lines?

a. 1.
b. 2.
c. 3.
d. 4.
e. None of the Above.

Question 17


class A {
  public static void main(String[] args) {
    int[][] a = {{1,2,3},{4,5,6},{7,8,9,10}};
    System.out.print(a[0][2]+","+a[1][0]+","+a[2][1]);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 3,4,8
b. Prints: 7,2,6
c. Compiler Error.
d. Runtime Error.
e. None of the Above.

Question 18


class A {public String toString() {return "A";}}
class B {
  public static void main(String[] arg) {
    A[]     a1 = new A[1];         // 1
    A[][]   a2 = new A[2][];       // 2
    A[][][] a3 = new A[3][][];     // 3
    a1[0] = new A();               // 4
    a2[0] = a2[1] = a1;            // 5
    a3[0] = a3[1] = a3[2] = a2;    // 6
    System.out.print(a3[2][1][0]); // 7
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: null
b. Prints: A
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Compile-time error at 3.
f. Compile-time error at 4.
g. Compile-time error at 5.
h. Compile-time error at 6.
i. Compile-time error at 7.
j. Run-time Exception
k. None of the Above


Copyright © 2002, Dan Chisholm
All rights reserved.
1